package com.adk.backend.service.impl;

import com.adk.backend.entity.LxCustomList;
import com.adk.backend.mapper.CustomListMapper;
import com.adk.backend.service.CustomListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 投递单管理服务实现类
 */
@Slf4j
@Service
public class CustomListServiceImpl implements CustomListService {
    
    @Autowired
    private CustomListMapper customListMapper;
    
    @Override
    public List<Map<String, Object>> getCustomList(Map<String, Object> params) {
        return customListMapper.selectCustomListWithCustomer(params);
    }
    
    @Override
    public Map<String, Object> getCustomListDetailById(Long id) {
        return customListMapper.selectCustomListDetailById(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCustomList(LxCustomList customList) {
        try {
            if (customList.getId() != null && customList.getId() > 0) {
                // 更新
                customListMapper.updateById(customList);
            } else {
                // 新增
                customListMapper.insert(customList);
            }
            return true;
        } catch (Exception e) {
            log.error("保存投递单失败", e);
            return false;
        }
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCustomList(Long id) {
        try {
            customListMapper.deleteById(id);
            return true;
        } catch (Exception e) {
            log.error("删除投递单失败", e);
            return false;
        }
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePodInfo(Long id, Integer recvType, String recvNo, String recvDate, String pod, String remarks, Integer userId) {
        try {
            LxCustomList customList = customListMapper.selectById(id);
            if (customList == null) {
                return false;
            }
            
            customList.setRecvType(recvType);
            customList.setRecvNo(recvNo);
            customList.setRecvDate(recvDate);
            if (recvDate != null && !recvDate.isEmpty()) {
                try {
                    LocalDate date = LocalDate.parse(recvDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    customList.setRecvTime((int) (date.atStartOfDay().atZone(java.time.ZoneId.systemDefault()).toEpochSecond()));
                } catch (Exception e) {
                    log.warn("日期格式转换失败: {}", recvDate, e);
                }
            }
            customList.setRecvUid(userId);
            customList.setRecvd(2); // 已签收
            customList.setPod(pod);
            customList.setRemarks(remarks);
            
            customListMapper.updateById(customList);
            return true;
        } catch (Exception e) {
            log.error("更新POD信息失败", e);
            return false;
        }
    }
    
    @Override
    public List<String> getMonthList() {
        List<String> months = new ArrayList<>();
        LocalDate startMonth = LocalDate.of(2023, 4, 1);
        LocalDate currentMonth = LocalDate.now().plusMonths(1);
        
        LocalDate month = startMonth;
        while (!month.isAfter(currentMonth)) {
            months.add(month.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            month = month.plusMonths(1);
        }
        
        // 反转列表，最新的月份在前
        java.util.Collections.reverse(months);
        return months;
    }
}

